package org.weasel.wizards;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.Wizard;
import org.weasel.business.Developer;
import org.weasel.business.Project;
import org.weasel.business.Release;
import org.weasel.business.Settings;
import org.weasel.business.Utilities;

public class ProjectWizard extends Wizard {
	private BasicProjectPage pageProjectName;
	private SettingsProjectPage pageReleasesAndIterations;
	private ISelection selection;

	
	public ProjectWizard() {
		super();
		setWindowTitle("New Project");
		setNeedsProgressMonitor(true);
	}
	
	@Override
	public void addPages() {
        pageProjectName = new BasicProjectPage(selection);
        addPage(pageProjectName);
        pageReleasesAndIterations = new SettingsProjectPage(selection);
        addPage(pageReleasesAndIterations);
    }

	@Override
	public boolean performFinish() {
		Settings sett = null;
		Project project = null;
		try {
			String projectName = pageProjectName.getProjectName();
			
			//TODO read the developers and create objects for them
//			ArrayList<String> developerNames = pageReleasesAndIterations.getDevelopers();
			
			sett = new Settings("./Settings.xml");
			//TODO add a control which holds the path of the project
			project = sett.createAndAddProject(projectName, "./" + projectName + ".xml");
			project.setName(projectName);
			for (Developer developer : pageReleasesAndIterations.getDevelopers()) {
				project.addDeveloper(developer);
			}
			
			int numberOfReleases = Integer.parseInt(pageReleasesAndIterations.getNumberOfReleases());
			int numberOfIterations = Integer.parseInt(pageReleasesAndIterations.getIterationsPerRelease());
			
			for (int i = 0; i < numberOfReleases; i++) {
				String releaseNumber = String.valueOf(i + 1);
				Release aRelease = project.createAndAddRelease("Release " + releaseNumber);
				
				for (int j = 0; j < numberOfIterations; j++) {
					String iterationNumber = String.valueOf(j + 1);
					aRelease.createAndAddIteration("Iteration " + iterationNumber);
				}
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		Utilities.callCreateAndUpdateTree();
		
		return true;
	}



}
